package com.wanmait.wanmarecruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wanmait.wanmarecruit.annotation.ManageLogAnnotation;
import com.wanmait.wanmarecruit.entity.Position;
import com.wanmait.wanmarecruit.entity.Province;
import com.wanmait.wanmarecruit.mapper.PositionMapper;
import com.wanmait.wanmarecruit.service.CityService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wanmarecruit.mapper.CityMapper;
import com.wanmait.wanmarecruit.entity.City;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements CityService {

    @Resource
    private CityMapper cityMapper;

    @Resource
    private PositionMapper positionMapper;

    @Override
    public List<City> getCityByProvince(Integer provinceId) {
        LambdaQueryWrapper<City> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(City::getProvId,provinceId);
        List<City> citys = cityMapper.selectList(queryWrapper);
        return citys;
    }

    @Override
    public List<City> getRandomSevenHotCity() {
        List<City> cities = cityMapper.selectRandomSevenHotCity();
        cities.forEach(city -> {
            LambdaQueryWrapper<Position> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Position::getCityId,city.getCityId());
            Integer positionCount = positionMapper.selectCount(queryWrapper).intValue();
            city.setPositionCount(positionCount);
        });
        return cities;
    }

    @Override
    public City getProvByCityId(Integer cityId) {
        LambdaQueryWrapper<City> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(City::getCityId,cityId);
        return this.getOne(queryWrapper);
    }
}
